﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using Common;
namespace DAGG.Ashx
{
    /// <summary>
    /// UpLoad 的摘要说明
    /// </summary>
    public class UpLoad : IHttpHandler
    {
       // JsonHelper jsonHelper = new JsonHelper();
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["oper"];
            string filePath = "";
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file = context.Request.Files[0];

                if (file.ContentLength > 0 && file.ContentType.IndexOf("image/") >= 0)
                {
                  
               
                    var fileName =string.Empty;
                    if (action=="temp")//预览
                      fileName = Path.Combine(context.Request.MapPath("~/TempImages"), Path.GetFileName(file.FileName));
                    if (action == "add")//保存
                    {
                        fileName = Path.Combine(context.Request.MapPath("~/upload"), Path.GetFileName(file.FileName));
                        //清空临时文件夹
                        foreach (string d in Directory.GetFileSystemEntries(context.Request.MapPath("~/TempImages")))
                        {
                            if (File.Exists(d))
                            {
                                FileInfo fi = new FileInfo(d);
                                if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                                    fi.Attributes = FileAttributes.Normal;
                                File.Delete(d);//直接删除其中的文件  
                            }
                        }
                    }
                    
                    filePath = fileName;
                    file.SaveAs(fileName);
                
                }
            }
            context.Response.Write(filePath); 
        }


        public MemoryStream ResizeImg(Stream ImgFile, int maxWidth, int maxHeight)
        {
            Image imgPhoto = Image.FromStream(ImgFile);

            decimal desiredRatio = Math.Min((decimal)maxWidth / imgPhoto.Width, (decimal)maxHeight / imgPhoto.Height);
            int iWidth = (int)(imgPhoto.Width * desiredRatio);
            int iHeight = (int)(imgPhoto.Height * desiredRatio);

            Bitmap bmPhoto = new Bitmap(iWidth, iHeight);

            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height), GraphicsUnit.Pixel);

            MemoryStream ms = new MemoryStream();
            bmPhoto.Save(ms, ImageFormat.Jpeg);

            imgPhoto.Dispose();
            gbmPhoto.Dispose();
            bmPhoto.Dispose();

            return ms;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}